﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunboxsoft.WinForm
{
    public class ValidationManager
    {
        private static List<ValidationEntry> s_entryList = new List<ValidationEntry>();

        public static void Register(IView view, IValidation validation)
        {
            ValidationEntry entry = GetOrCreate(view);
            for (int index = 0; index < entry.ValidationList.Count; index++)
            {
                if (entry.ValidationList[index] == validation)
                {
                    return;
                }
            }
            entry.ValidationList.Add(validation);
        }

        public static void Unregister(IView view, IValidation validataion)
        {
            for (int index = 0; index < s_entryList.Count; index++)
            {
                if (s_entryList[index].View == view)
                {
                    for (int vIndex = 0; vIndex < s_entryList[index].ValidationList.Count; vIndex++)
                    {
                        if (s_entryList[index].ValidationList[vIndex] == validataion)
                        {
                            s_entryList[index].ValidationList.RemoveAt(vIndex);
                            return;
                        }
                    }
                }
            }
        }

        public static void Remove(IView view)
        {
            for (int index = 0; index < s_entryList.Count; index++)
            {
                if (s_entryList[index].View == view)
                {
                    s_entryList.RemoveAt(index);
                    break;
                }
            }
        }

        private static ValidationEntry GetOrCreate(IView view)
        {
            ValidationEntry entry = null;
            for (int index = 0; index < s_entryList.Count; index++)
            {
                if (s_entryList[index].View == view)
                {
                    entry = s_entryList[index];
                    break;
                }
            }

            if (entry == null)
            {
                entry = new ValidationEntry(view);
                s_entryList.Add(entry);
            }
            return entry;
        }

        private static ValidationEntry Get(IView view)
        {
            ValidationEntry entry = null;
            for (int index = 0; index < s_entryList.Count; index++)
            {
                if (s_entryList[index].View == view)
                {
                    entry = s_entryList[index];
                    break;
                }
            }
            return entry;
        }

        public static bool Validate(IView view, out string message)
        {
            ValidationEntry entry = Get(view);
            if (entry != null)
            {
                for (int index = 0; index < entry.ValidationList.Count; index++)
                {
                    if (entry.ValidationList[index].Validate(out message) == false)
                    {
                        return false;
                    }
                }
            }
            message = null;
            return true;
        }
    }
}
